package pigshare.objects;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Blob;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.primefaces.model.DefaultStreamedContent;

import pigshare.beans.LoginBean;
import pigshare.database.DatabaseManager;

@ManagedBean
@RequestScoped
public class Foto {

	public java.sql.Blob pic;
	public String kommentar;
	public int id;
	public int user_FK;
	public String title;

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public int rating;
	public DefaultStreamedContent content;

	private String cookieValue;
	private Blob bl;
	private InputStream stream;
	private Logger log = Logger.getLogger(LoginBean.class);

	public Foto() {

	}

	public Foto(java.sql.Blob pic, String kommentar, int id, int userFK,
			String title, int rating, DefaultStreamedContent content) {
		this.pic = pic;
		this.kommentar = kommentar;
		this.id = id;
		this.user_FK = userFK;
		this.title = title;
		this.rating = rating;
		this.content = content;
	}

	public java.sql.Blob getPic() {
		return pic;
	}

	public void setPic(java.sql.Blob blob) {
		this.pic = blob;
	}

	public String getKommentar() {
		return kommentar;
	}

	public void setKommentar(String kommentar) {
		this.kommentar = kommentar;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getUser_FK() {
		return user_FK;
	}

	public void setUser_FK(int user_FK) {
		this.user_FK = user_FK;
	}

	public String getItle() {
		return title;
	}

	public void setItle(String itle) {
		this.title = itle;
	}

	public int getRating() {
		return rating;
	}

	public void setRating(int rating) {
		this.rating = rating;
	}

	public DefaultStreamedContent getContent() {
		if (content == null) {
			HttpServletRequest httpServletRequest = (HttpServletRequest) FacesContext
					.getCurrentInstance().getExternalContext().getRequest();
			Cookie[] cookies = httpServletRequest.getCookies();

			if (cookies != null) {
				for (int i = 0; i < cookies.length; i++) {
					if (cookies[i].getName().equalsIgnoreCase("id")) {
						cookieValue = cookies[i].getValue();
					}
				}
			}
			log.info("foto id: " + id);
			ResultSet rs = DatabaseManager.getDatabaseManager().sendQuery(
					"SELECT  `blob` FROM posts WHERE  `id` = " + id +";");
			try {
				if (rs.next()) {
					bl = rs.getBlob("blob");
					log.info("length of the picture " + id + " " + bl.length());
					stream = bl.getBinaryStream();
					// /* use your database call here */
					// BufferedInputStream in = new
					// BufferedInputStream(ImageBean.class
					// .getClassLoader().getResourceAsStream("test/test.png"));
					BufferedInputStream in = new BufferedInputStream(stream);
					ByteArrayOutputStream out = new ByteArrayOutputStream();

					int val = -1;
					/*
					 * this is a simple test method to double check values from
					 * the stream
					 */
					try {
						while ((val = in.read()) != -1)
							out.write(val);
					} catch (IOException e) {
						e.printStackTrace();
					}

					byte[] bytes = out.toByteArray();
					content = new DefaultStreamedContent(
							new ByteArrayInputStream(bytes), "image/png",
							"test.png");
				}
			} catch (SQLException e1) {
				e1.printStackTrace();
			}

		}

		return content;
	}

	public void setContent(DefaultStreamedContent content) {
		this.content = content;
	}

}
